package com.caiheng.api.service.asset.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.caiheng.api.dao.asset.EstateAssetBorrowRecordMapper;
import com.caiheng.api.dao.asset.EstateAssetMapper;
import com.caiheng.api.entity.asset.EstateAsset;
import com.caiheng.api.entity.asset.EstateAssetBorrowRecord;
import com.caiheng.api.entity.sys.SysUser;
import com.caiheng.api.entity.wxuser.User;
import com.caiheng.api.exception.TransactionalException;
import com.caiheng.api.service.asset.IEstateAssetBorrowRecordService;
import com.caiheng.api.service.base.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 资产借用记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-20
 */
@Service
public class EstateAssetBorrowRecordServiceImpl extends BaseServiceImpl<EstateAssetBorrowRecordMapper, EstateAssetBorrowRecord> implements IEstateAssetBorrowRecordService {

    @Resource
    private EstateAssetBorrowRecordMapper assetBorrowRecordMapper;

    @Resource
    private EstateAssetMapper assetMapper;


    @Override
    @Transactional
    public int borrow(User user, SysUser sysUser, EstateAsset asset) {
        int ret = 0;
        try{

            EstateAssetBorrowRecord lastBorrowRecord = null;
            //资产状态 0-正常 1-维修 2-报废 3-借出
            if(asset.getState() == 3){//当前是借出状态
                QueryWrapper<EstateAssetBorrowRecord> qw = new QueryWrapper<>();
                qw.eq("asset_id", asset.getId());
                qw.orderByDesc("create_time");
                qw.last("limit 1");
                lastBorrowRecord = assetBorrowRecordMapper.selectOne(qw);
            }
            asset.setState(3);
            ret = assetMapper.updateById(asset);
            if(ret <= 0){
                throw new TransactionalException("assetMapper updateById error");
            }

            if(lastBorrowRecord!= null) {
                lastBorrowRecord.setState(1);
                lastBorrowRecord.setRebackTime(new Date());
                lastBorrowRecord.setRebackRemark("资产被"+sysUser.getNickName()+"直接借走，系统自动归还");
                ret = assetBorrowRecordMapper.updateById(lastBorrowRecord);
                if(ret <= 0){
                    throw new TransactionalException("assetBorrowRecordMapper updateById error");
                }
            }


            EstateAssetBorrowRecord borrowRecord = new EstateAssetBorrowRecord();
            if(lastBorrowRecord!= null){
                borrowRecord.setFromRecordId(lastBorrowRecord.getId());
            }else{
                borrowRecord.setFromRecordId(0);
            }
            borrowRecord.setPropertyId(asset.getPropertyId());
            borrowRecord.setCreateTime(new Date());
            borrowRecord.setWxUserId(user.getId());
            borrowRecord.setSysUserId(sysUser.getUserId());
            borrowRecord.setRealName(sysUser.getNickName());
            borrowRecord.setNickName(user.getNickName());
            borrowRecord.setPhoneNum(sysUser.getPhoneNum());
            borrowRecord.setAssetId(asset.getId());
            borrowRecord.setState(0);
            ret = assetBorrowRecordMapper.insert(borrowRecord);
            if(ret <= 0){
                throw new TransactionalException("assetBorrowRecordMapper insert error");
            }

        }catch (Exception e){
            e.printStackTrace();
            throw new TransactionalException("borrow system error");
        }
        return ret;
    }

    @Override
    @Transactional
    public int reback(EstateAsset asset, EstateAssetBorrowRecord record) {
        int ret = 0;
        try{
            //资产状态 0-正常 1-维修 2-报废 3-借出
            asset.setState(0);
            ret = assetMapper.updateById(asset);
            if(ret <= 0){
                throw new TransactionalException("assetMapper updateById error");
            }

            ret = assetBorrowRecordMapper.updateById(record);
            if(ret <= 0){
                throw new TransactionalException("assetBorrowRecordMapper updateById error");
            }

        }catch (Exception e){
            e.printStackTrace();
            throw new TransactionalException("reback system error");
        }
        return ret;
    }
}
